package com.qmx.launcher.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.qmx.launcher.R;
import com.qmx.launcher.view.DrawBoardView;

import java.lang.reflect.Method;

public class WhiteBoardActivity extends Activity {
    static int i = 0, j = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_white_board);

    }

    // 创建选项菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        super.onCreateOptionsMenu(menu);
        setIconsVisible(menu, true);
        MenuInflater inflator = new MenuInflater(this);//实例化一个MenuInflater对象
        inflator.inflate(R.menu.toolsmenu, menu);    //解析菜单文件
        return true;
    }

    private void setIconsVisible(Menu menu, boolean flag) {
        //判断menu是否为空
        if (menu != null) {
            try {
                //如果不为空,就反射拿到menu的setOptionalIconsVisible方法
                Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
                Method method = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
                //暴力访问该方法
                method.setAccessible(true);
                //调用该方法显示icon
                method.invoke(menu, flag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    // 当菜单项被选择时，作出相应的处理
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        DrawBoardView dv = (DrawBoardView) findViewById(R.id.drawView1);    //获取自定义的绘图视图
        dv.paint.setXfermode(null);        //取消擦除效果
        dv.paint.setStrokeWidth(5);        //初始化画笔的宽度
        switch (item.getItemId()) {
            case R.id.red:
                dv.paint.setColor(Color.RED);    //设置画笔的颜色为红色
                item.setChecked(true);
                break;
            case R.id.green:
                dv.paint.setColor(Color.GREEN);    //设置画笔的颜色为绿色
                item.setChecked(true);
                break;
            case R.id.blue:
                dv.paint.setColor(Color.BLUE);    //设置画笔的颜色为蓝色
                item.setChecked(true);
                break;
            case R.id.width_1:
                dv.paint.setStrokeWidth(5);    //设置笔触的宽度为1像素
                break;
            case R.id.width_2:
                dv.paint.setStrokeWidth(10);    //设置笔触的宽度为5像素
                break;
            case R.id.width_3:
                dv.paint.setStrokeWidth(15);//设置笔触的宽度为10像素
                break;
            case R.id.clear:
                dv.clear();        //橡皮檫
                break;
            case R.id.back:
                dv.back();        //返回上一步
                break;
            case R.id.nothing:
                dv.nothing();        //清屏
                //DrawView dv1 = (DrawView) findViewById(R.id.drawView1);
                break;
            case R.id.save: {
                j = 0;
                dv.save(i, j);
                i++;    //保存绘画到APP目录
                break;
            }
            case R.id.save_camera: {
                j = 1;
                dv.save(i, j);
                i++;
                if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))//若是获取权限失败，提示手动获取
                    Toast.makeText(WhiteBoardActivity.this, "获取权限失败，请手动授权", Toast.LENGTH_SHORT).show();
                break;
            }//保存绘画到相册

        }
        return true;
    }
}
